package org.grottarossa.ui.core.casemanagement.menu;

import org.grottarossa.template.data.pojo.Menu;
import org.grottarossa.template.drivers.DriverException;
import org.grottarossa.ui.core.ApplicationSession;
import org.grottarossa.ui.core.container.MenuContainer;



import com.vaadin.data.Container;
import com.vaadin.event.ItemClickEvent.ItemClickListener;
import com.vaadin.ui.AbstractSelect;
import com.vaadin.ui.Tree;

public class FunctionTreeMenu extends Tree
{
	/**
	 *
	 */
	private static final long serialVersionUID = 6640009791807101462L;
	private final ApplicationSession m_appSession;

	public FunctionTreeMenu(ApplicationSession appSession, ItemClickListener listener, String sApp, String sMenu) throws DriverException
	{
		this.m_appSession = appSession;
		Menu menu = m_appSession.getBuilder().getPortalManagement().getMenu(sApp, sMenu);
		draw(listener, sApp, new MenuContainer(menu, sMenu), "caption");
    }

	public FunctionTreeMenu(ApplicationSession appSession, ItemClickListener listener, String sApp, Container treeDataSource, String captionPropertyId)
	{
		m_appSession = appSession;
		this.draw(listener, sApp, treeDataSource, captionPropertyId);
	}

	private void draw(ItemClickListener listener, String sApp, Container treeDataSource, String captionPropertyId) {
		setContainerDataSource(treeDataSource);
		setItemCaptionPropertyId(captionPropertyId);
		setItemCaptionMode(AbstractSelect.ITEM_CAPTION_MODE_PROPERTY);

		for (Object id : rootItemIds()) {
            expandItemsRecursively(id);
        }
		setSelectable(true);
		setMultiSelect(false);
        setNullSelectionAllowed(false);
        addListener((ItemClickListener) listener);
	}
}